<template>
  <div class="personal-center">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="top-nav">
        <div class="nav-container">
          <div class="logo">
            <img src="./me-img/logo.png" alt="宠物商城" />
          </div>
          <router-link to="/" class="nav-item">首页</router-link>
          <router-link to="/type" class="nav-item">商品分类</router-link>
          <router-link to="/carts" class="nav-item">购物车</router-link>
          <a href="" class="nav-item active">个人中心</a>
        </div>
      </div>
      <div class="call">
        <router-link to="/service" class="call-item"><img src="./me-img/call.png" alt="" class="call-img" />客服</router-link>
      </div>
    </header>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 上半部分：个人信息 -->
      <div class="all">
        <div class="profile-section">
          <div class="user-info">
            <div class="touxiang-img">
              <img :src="userAvatar || './me-img/touxiang.jpg'" alt="头像" class="touxiangimg" />
            </div>
            <div class="meinfor">
              <h3>{{ userName }}</h3>
              <p>账号：123456</p>
              <p>宠物：{{ userPetname }}</p>
            </div>
          </div>
          <div class="white-one">
            <a @click="navigateToOrderList2('couPon')" class="coupon-info">
              <h3>优惠卷</h3>
            </a>
            <a @click="navigateToAccountManagement()" class="wallet-info">
              <h3>账号管理</h3>
            </a>
          </div>
        </div>
        <div class="pet-photo">
          <div class="photo-wall-header">
            <h3>我的宠物照片墙</h3>
            <div>
              <input type="file" id="fileInput" ref="fileInput" style="display: none" accept="image/*" @change="handleFileUpload" multiple />
              <button class="add-photo-btn" @click="triggerFileInput">+ 添加照片</button>
            </div>
          </div>
          <div class="photo-wall-container">
            <div class="photo-wall" ref="photoWall">
              <div class="photo-item" v-for="(photo, index) in petPhotos" :key="index">
                <img :src="photo.url" alt="宠物照片" class="petphotos" />
                <button class="delete-btn" @click.stop="removePhoto(index)">×</button>
              </div>
            </div>
            <button class="scroll-btn left" @click="scrollLeft">‹</button>
            <button class="scroll-btn right" @click="scrollRight">›</button>
          </div>
        </div>
      </div>

      <!-- 下半部分：功能区域 -->
      <div class="function-section">
        <!-- 左侧功能 -->
        <!-- 修改左侧功能项的链接 -->
        <div class="left-functions">
          <a @click="navigateToOrderList('pendingPayment')" class="function-item1">
            <img src="./me-img/付款.png" alt="代付款" class="function-img" />
            <h3>待付款</h3>
          </a>
          <a @click="navigateToOrderList('pendingReview')" class="function-item1">
            <img src="./me-img/评价.png" alt="待评价" class="function-img" />
            <h3>待评价</h3>
          </a>
          <a @click="navigateToOrderList('pendingDelivery')" class="function-item1">
            <img src="./me-img/快递.png" alt="待收货" class="function-img" />
            <h3>待收货</h3>
          </a>
          <a @click="navigateToOrderList('afterSale')" class="function-item1">
            <img src="./me-img/售后.png" alt="退换/换货" class="function-img" />
            <h3>退换/售后</h3>
          </a>
        </div>

        <!-- 右侧功能 -->
        <div class="right-functions">
          <a @click="navigateToOrderList1('browseFootprints')" class="function-item2"
            ><img src="./me-img/足迹.png" alt="足迹" class="function-img2" />
            <h3>浏览记录</h3></a
          >
          <a @click="navigateToOrderList1('collectProducts')" class="function-item2"
            ><img src="./me-img/商品.png" alt="商品收藏" class="function-img2" />
            <h3>商品收藏</h3></a
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import emitter from "@/main";

export default {
  name: "PersonalCenter",

  data() {
    return {
      petPhotos: [{ url: "/mephotos/01.png" }, { url: "/mephotos/02.png" }, { url: "/mephotos/03.png" }, { url: "/mephotos/04.png" }],
      userAvatar: localStorage.getItem("userAvatar") || "123456",
      userName: localStorage.getItem("userNickname") || "小猫勇闯世界",
      userPetname: localStorage.getItem("userPetname") || "猫猫",
    };
  },

  created() {
    // 监听用户信息更新事件
    emitter.on("userInfoUpdated", this.handleUserInfoUpdate);

    // 初始化时从本地存储加载数据
    if (localStorage.getItem("userAvatar")) {
      this.userAvatar = localStorage.getItem("userAvatar");
    }
    if (localStorage.getItem("userNickname")) {
      this.userName = localStorage.getItem("userNickname");
    }
    if (localStorage.getItem("userPetname")) {
      this.userPetname = localStorage.getItem("userPetname");
    }
  },
  beforeUnmount() {
    // 移除事件监听
    emitter.off("userInfoUpdated", this.handleUserInfoUpdate);
  },

  methods: {
    handleUserInfoUpdate(data) {
      this.userAvatar = data.avatar;
      this.userName = data.nickname;
      this.userPetname = data.petname;
    },

    navigateToOrderList(tab) {
      this.$router.push({
        name: "order-list", // 使用路由名称而不是路径
        query: { tab },
      });
    },

    navigateToOrderList1(tab) {
      this.$router.push({
        name: "order-list1", // 使用路由名称而不是路径
        query: { tab },
      });
    },

    navigateToOrderList2(tab) {
      this.$router.push({
        name: "order-list2", // 使用路由名称而不是路径
        query: { tab },
      });
    },

    navigateToAccountManagement() {
      this.$router.push({
        name: "Account_Management", // 使用路由名称而不是路径
      });
    },
    triggerFileInput() {
      this.$refs.fileInput.click();
    },

    handleFileUpload(event) {
      const files = event.target.files;
      if (!files.length) return;

      for (let i = 0; i < files.length; i++) {
        const file = files[i];
        if (!file.type.match("image.*")) continue;

        const reader = new FileReader();
        reader.onload = (e) => {
          this.petPhotos.push({
            url: e.target.result,
            file: file, // 保存文件对象，便于后续上传
          });
        };
        reader.readAsDataURL(file);
      }

      // 清空input，以便可以重复选择同一文件
      event.target.value = "";
    },
    removePhoto(index) {
      this.petPhotos.splice(index, 1);
    },
    scrollLeft() {
      this.$refs.photoWall.scrollBy({ left: -200, behavior: "smooth" });
    },
    scrollRight() {
      this.$refs.photoWall.scrollBy({ left: 200, behavior: "smooth" });
    },
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
}

.personal-center {
  min-height: 100vh;
  background-color: #f5f5f5;
  min-width: 1200px; /* 添加最小宽度防止布局压缩 */
  overflow-x: auto; /* 允许横向滚动 */
}

/* 顶部导航栏样式 */

.top-nav {
  width: 100%;
  background-color: #f4efd2;
  color: black;
  padding: 15px 0;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: #bdbbb7 0px 0px 5px;
}

.nav-container {
  width: 90%;
  margin: 0 auto;
  display: flex;
  align-items: center;
}

.logo {
  margin-right: 40px;
  height: 40px;
}

img {
  height: 100%;
}

.nav-item {
  width: 60%;
  height: 40px;
  text-align: center;
  padding: 8px 40px;
  margin-right: 15px;
  cursor: pointer;
  font-size: 16px;
  border-radius: 18px;
  transition: all 0.3s;
}

.nav-item:hover {
  background-color: rgba(0, 0, 0, 0.2);
}

.nav-item.active {
  background-color: #d9c596;
  font-weight: bold;
}

.call {
  background-color: #f9f7e8;
  height: 30px;
  padding: 8px;
  min-width: 1200px; /* 保持客服栏不换行 */
}

a {
  text-decoration: none;
  color: black;
}
.call-item {
  float: right;
}

.call-img {
  height: 18px;
}

/* 主要内容区域 */

.main-content {
  width: 60%;
  margin: 0px auto;
  background-color: white;
  min-width: 1000px; /* 保持主要内容区域不换行 */
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  border-radius: 10px;
  overflow: hidden;
}

/* 个人信息区域 */

.all {
  height: 700px;
  border-bottom: 1px solid #eee;
  background-color: #f4efd2;
  padding: 10px;
  border-radius: 10px;
  box-shadow: #bdbbb7 0px 0px 5px;
}

.profile-section {
  display: flex;
  padding: 20px;
  height: 300px;
}

.user-info {
  text-align: center;
  width: 60%;
  padding: 10px;
  display: flex;
}

.touxiang-img {
  height: 120px;
  width: 120px;
  margin: 30px 20px;
}

.touxiangimg {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
}

.meinfor {
  width: 30%;
  text-align: left;
  padding-top: 40px;
  padding-left: 20px;
}

.meinfor h3 {
  font-size: 20px;
  margin-bottom: 15px;
}

.meinfor p {
  font-size: 16px;
  margin: 10px 0;
  color: #666;
}

.white-one {
  background-color: white;
  display: flex;
  width: 40%;
  padding: 10px;
  border-radius: 10px;
}

.coupon-info,
.wallet-info {
  width: 50%;
  text-align: center;
  padding: 80px 0px;
  margin: 0px 5px;
  border-radius: 8px;
  background-color: #f4efd2;
}

h3 {
  margin-bottom: 10px;
  color: #333;
  font-size: 16px;
}

p {
  color: #666;
  font-size: 14px;
  margin: 5px 0;
}

.pet-photo {
  background-color: white;
  padding: 20px;
  margin: 10px 20px;
  height: 350px;
}

/* 功能区域 */
.function-section {
  display: flex;
  height: 350px;
  margin: 10px 0px;
}

.left-functions,
.right-functions {
  background-color: #f4efd2;
  border-radius: 10px;
  box-shadow: #bdbbb7 0px 0px 5px;
}

.left-functions {
  width: 80%;
  display: flex;
  justify-content: space-around;
  padding: 40px 0px;
  margin: 0px 10px 0px 0px;
}

.right-functions {
  width: 20%;
  display: flex;
  flex-direction: column; /* 垂直排列 */
  justify-content: center; /* 垂直方向居中 */
  align-items: center; /* 水平方向居中 */
}

.function-item1 {
  width: 20%;
  text-align: center;
  padding: 90px 0px;
  margin: 10px;
  background-color: #ffffff;
  cursor: pointer;
  transition: all 0.3s;
}

.function-img {
  width: 50%;
  aspect-ratio: 1 / 1;
}

.function-img2 {
  width: 30%;
  height: 50%;
}

.function-item2 {
  height: 25%;
  aspect-ratio: 1 / 1; /* 宽高比 1:1（正方形） */
  text-align: center;
  margin: 10px;
  cursor: pointer;
  border-radius: 50%;
  padding: 20px 0px;
  background-color: #ffffff;
}

.photo-wall-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.add-photo-btn {
  background-color: #d9c596;
  border: none;
  padding: 8px 15px;
  border-radius: 20px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.3s;
}

.add-photo-btn:hover {
  background-color: #c5b082;
}

.photo-wall-container {
  position: relative;
  height: 300px;
}

.photo-wall {
  display: flex;
  overflow-x: auto;
  scroll-behavior: smooth;
  height: 100%;
  padding: 10px;
  gap: 15px;
  -ms-overflow-style: none; /* IE and Edge */
  scrollbar-width: none; /* Firefox */
}

.photo-wall::-webkit-scrollbar {
  display: none; /* Chrome, Safari and Opera */
}

.photo-item {
  position: relative;
  flex: 0 0 auto;
  width: 250px;
  height: 100%;
}

.petphotos {
  width: 100%;
  height: 90%;
  object-fit: cover;
  border-radius: 10px;
  cursor: pointer;
  transition: transform 0.3s;
}

.petphotos:hover {
  transform: scale(1.03);
}

.delete-btn {
  position: absolute;
  top: 5px;
  right: 5px;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 16px;
  line-height: 25px;
}

.scroll-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.3);
  color: white;
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 20px;
  z-index: 10;
}

.scroll-btn.left {
  left: 10px;
}

.scroll-btn.right {
  right: 10px;
}

.scroll-btn:hover {
  background-color: rgba(0, 0, 0, 0.6);
}

/* 响应式调整 - 仅缩放不改变布局 */
@media (max-width: 1600px) {
  .main-content {
    transform: scale(0.9);
    transform-origin: top center;
  }
}

@media (max-width: 1400px) {
  .main-content {
    transform: scale(0.8);
  }
}

@media (max-width: 1200px) {
  .personal-center {
    overflow-x: scroll; /* 小于1200px时启用横向滚动 */
  }
}
</style>
